#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'

from datetime import datetime

from golsee import db


class Settings(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    value = db.Column(db.String(200))
    type = db.Column(db.String(20))
    is_use = db.Column(db.Integer, default=1)
    seq = db.Column(db.Integer, default=1)
    icon = db.Column(db.String(50))

    @classmethod
    def admin_second_bar(cls):
        list_bar = Settings.query.filter(Settings.type == 'admin_second_bar', Settings.is_use == 1).order_by(
            Settings.seq)
        return list_bar

    @classmethod
    def blog_name(cls):
        blog_name = Settings.query.filter(Settings.type == 'blog_name', Settings.is_use == 1).first()
        if blog_name:
            return blog_name.name
        else:
            return 'golsee'

    @classmethod
    def first_bar(cls):
        first_bar = Settings.query.filter(Settings.type == 'first_bar', Settings.is_use == 1).order_by(Settings.seq)
        return first_bar

    @classmethod
    def blog_setting(cls):
        d = dict()
        settings = Settings.query.filter(Settings.seq == 0, Settings.is_use == 1).all()
        for setting in settings:
            d[setting.type] = setting.value
        return d


class Blog_info(db.Model):
    '''blog信息汇总'''
    date = db.Column(db.String(10), primary_key=True, index=True)
    visit_all = db.Column(db.Integer)
    visit_day = db.Column(db.Integer)
    visit_month = db.Column(db.Integer)
    visit_attack = db.Column(db.Integer)
    visit_attack_day = db.Column(db.Integer)
    visit_robot = db.Column(db.Integer)
    visit_robot_day = db.Column(db.Integer)
    article_all = db.Column(db.Integer)
    article_month = db.Column(db.Integer)
    user_all = db.Column(db.Integer)
    login_all = db.Column(db.Integer)


    def __init__(self):
        pass

    def __repr__(self):
        return '<Date: %s>' % self.date


    @classmethod
    def get_info_by_day(cls, date):
        '''根据日期返回记录,没有就新增'''
        info = Blog_info.query.order_by(Blog_info.date.desc()).first()
        if info is None:
            info = Blog_info()
            info.date = date
            info.visit_all = 0
            info.visit_day = 0
            info.visit_month = 0
            info.visit_attack = 0
            info.visit_attack_day = 0
            info.visit_robot = 0
            info.visit_robot_day = 0
            info.article_all = 0
            info.article_month = 0
            info.user_all = 0
            info.login_all = 0
            db.session.add(info)
            db.session.commit()
            info = Blog_info.query.filter_by(date=date).first()
        else:
            if info.date < date:
                new = Blog_info()
                new.date = date
                new.visit_day = 0
                new.visit_attack_day = 0
                new.visit_robot_day = 0
                new.visit_all = info.visit_all
                new.visit_attack = info.visit_attack
                new.visit_robot = info.visit_robot
                new.article_all = info.article_all
                new.user_all = info.user_all
                new.login_all = info.login_all
                if info.date[5:7] != date[5:7]:
                    new.visit_month = 0
                    new.article_month = 0
                else:
                    new.visit_month = info.visit_month
                    new.article_month = info.article_month
                db.session.add(new)
                db.session.commit()
                info = Blog_info.query.filter_by(date=date).first()
            elif info.date > date:
                info = Blog_info.get_info_by_day(date)

        return info


    @classmethod
    def info(cls):
        '''实时信息'''
        old = Blog_info.query.order_by(Blog_info.date.desc()).first()
        return old


    @classmethod
    def new_visit(cls, date):
        '''正常访问+1'''
        info = Blog_info.get_info_by_day(date)
        info.visit_day += 1
        if info.date[5:7] == str(datetime.now().date())[5:7]:
            info.visit_month += 1
        else:
            info.visit_month = 1
        info.visit_all += 1
        db.session.add(info)
        db.session.commit()


    @classmethod
    def new_robot_visit(cls, date):
        '''机器人访问+1'''
        info = Blog_info.get_info_by_day(date)
        info.visit_robot += 1
        info.visit_robot_day += 1
        db.session.add(info)
        db.session.commit()


    @classmethod
    def new_attack_visit(cls, date):
        '''疑似攻击+1'''
        info = Blog_info.get_info_by_day(date)
        info.visit_attack += 1
        info.visit_attack_day += 1
        db.session.add(info)
        db.session.commit()


    @classmethod
    def new_article(cls):
        '''文章+1'''
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.article_all += 1
        old.article_month += 1
        db.session.add(old)
        db.session.commit()


    @classmethod
    def new_user(cls):
        '''用户+1'''
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.user_all += 1
        db.session.add(old)
        db.session.commit()


    @classmethod
    def new_login(cls):
        '''登陆+1'''
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.login_all += 1
        db.session.add(old)
        db.session.commit()

    @classmethod
    def del_user(cls):
        """用户-1"""
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.user_all -= 1
        db.session.add(old)
        db.session.commit()

    @classmethod
    def del_article(cls):
        """文章-1"""
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.article_all -= 1
        db.session.add(old)
        db.session.commit()

